#!/bin/sh

MODDIR=/lib/modules
boxtype=`cat /etc/boxtype`

if [ $boxtype == 'bska' ]; then
  echo "Init VFD bialy"
  insmod $MODDIR/vfd.ko rec=0
else
  echo "Init VFD czarny"
  insmod $MODDIR/vfd.ko rec=1
fi


if [ -e /var/etc/.firstboot ]; then
   echo "WAIT 3 min" > /dev/vfd
   echo "WAIT 3 min"
   rm /var/etc/.firstboot
   touch /dev/.devfsd
fi

   /etc/init.d/mountvirtfs start
   /etc/init.d/portmap start

   insmod $MODDIR/e2_proc.ko

#start automounter with umount timeout of 5 seconds
   insmod $MODDIR/autofs4.ko
   /usr/sbin/automount --timeout=5 /autofs file /etc/auto.usb

if [ ! -e /var/etc/.firstboot ]; then

   echo "Starting DEBUG Interface"
   /etc/init.d/mountall start
   /etc/init.d/hostname start

   echo "starting telnetd"
   /usr/sbin/telnetd -l /bin/login

   echo "init stmfb"
   insmod $MODDIR/stmcore-display-stx7100.ko
   insmod $MODDIR/stmfb.ko display0=1280x720-32@50:12m:pal:yuv:yuv;# display1=720x576-16@50:1m:pal:cvbs;

   echo "LOADING MODULES"   
   echo "LOADING MODULES" >/dev/vfd

   if [ ! -e /boot/audio.elf ]; then
      echo "FW MISSING!!!"
   fi
   echo "load a firmware"
   /bin/ustslave /dev/st231-1 /boot/audio.elf
   echo "load v firmware"
   /bin/ustslave /dev/st231-0 /boot/video.elf

   echo "init embx"
   insmod $MODDIR/embxshell.ko
   insmod $MODDIR/embxmailbox.ko mailbox0=0xb9211000:136:set2 mailbox1=0xb9212000:0 ;
   insmod $MODDIR/embxshm.ko mailbox0=shm:0:7:0x60000000:0:256:256:0:1024:0xa4000000:0x10000000 ;
   insmod $MODDIR/mme_host.ko transport0=shm
   
   if [ $boxtype == 'bsla' ]; then
   echo "init Cooler"
    insmod $MODDIR/cooler.ko
   fi

   echo "init AVS"
   insmod $MODDIR/avs.ko type=stv6412

   echo "init player"
   player=`cat /proc/stb/player/version`
   player=`cat /proc/stb/player/version | awk '{ print substr( $0, length($0) - 2, length($0) ) }'`
   echo "init player $player"
   insmod $MODDIR/mmelog.ko
   insmod $MODDIR/mpeg2hw.ko mmeName=MPEG2_TRANSFORMER0;
   insmod $MODDIR/ksound.ko
   insmod $MODDIR/p2div64.ko
   insmod $MODDIR/stm_v4l2.ko
   if [ $player -gt 131 ]; then
	insmod $MODDIR/stmvout.ko
	insmod $MODDIR/stmvbi.ko
   fi
   insmod $MODDIR/pseudocard.ko
   
    if [ $boxtype == 'bska' ]; then
         echo "init frontends bialy"
         insmod $MODDIR/fe-core.ko tuner=bialy ;
    else
         echo "init frontends czarny"
	 insmod $MODDIR/fe-core.ko tuner=czarny ;
    fi

   echo "init drivers"
   if [ $player -gt 131 ]; then
	insmod $MODDIR/pti.ko
	insmod $MODDIR/stm_monitor.ko
	insmod $MODDIR/stmsysfs.ko
    	insmod $MODDIR/stmdvb.ko
   else
	insmod $MODDIR/pti.ko
	insmod $MODDIR/stmdvb.ko
	insmod $MODDIR/stm_monitor.ko
   fi	
   insmod $MODDIR/player2.ko
   insmod $MODDIR/sth264pp.ko
   insmod $MODDIR/stmalloc.ko
   insmod $MODDIR/platform.ko
   insmod $MODDIR/smartcard.ko

   mknod /dev/sci0 c 169 0
   mknod /dev/sci1 c 169 1

   showiframe -p /boot/bootlogo.mvi &
   stfbcontrol a 0

   echo "init frontpanel"
   insmod $MODDIR/boxtype.ko
   insmod $MODDIR/simu_button.ko

   echo "init lircd"
   /usr/bin/lircd

   mknod /dev/rc c 147 1 
   
   /bin/evremote2 &

   /etc/init.d/mountall start
   /etc/init.d/hostname start

   echo "starting telnetd"
   /usr/sbin/telnetd -l /bin/login
   /etc/init.d/vsftpd start
   /usr/sbin/inetd

   echo "LOAD e2 PKT MOD" > /dev/vfd
   echo "LOAD e2 PKT MOD"

   #init rc.modules
   /var/config/rc.modules &

   #init rc.local
   /var/config/rc.local &

   until false
   do
      echo "starting e2->"
      /etc/init.d/networking restart
      /var/crossepg/crossepg_epgmove.sh
      /usr/local/bin/enigma2
      rtv=$?
      cp /tmp/reference.dat /var/bin
      echo "e2 ended <- RTV: " $rtv
      case "$rtv" in
		0) echo "0"
		   echo "E2 ERROR" > /dev/vfd
		   init 6
		   sync
		   reboot;;
		1) echo "1"
		   echo "SHUTDOWN" > /dev/vfd
		   init 6
		   umount -a;;
		2) echo "2"
		   echo "REBOOT" > /dev/vfd
		   init 6
		   sync
		   reboot;;
		3) echo "3"
		   echo "E2 RESTART" > /dev/vfd
		   echo "0" > /proc/progress
		   showiframe -p /boot/bootlogo.mvi &;;
		4) echo "4"
		   echo "Moderate E2 standby" > /dev/vfd
		   sync
		   /bin/vdstandby -n
		   echo "0" > /proc/progress
		   showiframe -p /boot/bootlogo.mvi &;;
		*) echo "*"
		   echo "ERROR" > /dev/vfd
		   init 6
		   sync
		   echo "0" > /proc/progress
		   showiframe -p /boot/bootlogo.mvi &
      esac
   done 
fi

#never reached
echo "REBOOT" > /dev/vfd
echo "REBOOT"
sync
reboot
